<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/15 11:48
 * Desc: 会员联系地址
 */
class  Service_Api_Member_Address extends Contract_Api_Member_Address
{
    private $table_name = 'member_address';

    private $fields = array('id', 'province', 'city', 'area', 'address', 'postcode', 'receiver', 'phone', 'is_default');

    public function list(int $mid, array $params = array()): array
    {
        /**
         * @var $page
         * @var $pagesize
         */
        extract($params);

        $builder = DB::select_array($this->fields)
            ->from($this->table_name)
            ->where('memberid', '=', $mid);

        $total = $builder->execute()->count();

        $builder->order_by('id', 'desc');
        if (!empty($page) && !empty($pagesize)) {
            $offset = St_Page::offset($page, $pagesize);
            $builder->offset($offset)->limit($pagesize);
        }

        $list = $builder->execute()->as_array();

        return array(
            'total' => $total,
            'list' => $list
        );
    }


    public function info(int $mid, int $id)
    {
        $info = DB::select_array($this->fields)
            ->from($this->table_name)
            ->where('memberid', '=', $mid)
            ->where('id', '=', $id)
            ->execute()
            ->current();
        return $info ?: '';
    }


    public function edit(int $mid, array $params = array()): array
    {
        /**
         * @var $receiver
         * @var $phone
         * @var $province
         * @var $city
         * @var $area
         * @var $address
         * @var $postcode
         * @var $is_default
         * @var $id
         */
        extract($params);

        if (!empty($id)) {
            $info = ORM::factory('member_address')
                ->where('id', '=', $id)
                ->where('memberid', '=', $mid);
            if (!$info->find()->as_array()['id']) {
                throw new Exception('地址不存在');
            }
        } else {
            $info = ORM::factory($this->table_name);
        }
        $info->memberid   = $mid;
        $info->receiver   = $receiver;
        $info->phone      = $phone;
        $info->province   = $province;
        $info->city       = $city;
        $info->area       = $area;
        $info->address    = $address;
        $info->postcode   = $postcode;
        $info->is_default = $is_default;
        $info->save();
        if ($info->saved()) {
            $info->reload();
            if($is_default == 1) {
                DB::update($this->table_name)
                    ->where('memberid', '=', $mid)
                    ->where('is_default', '=', 1)
                    ->where('id', '!=', $info->id)
                    ->set(array('is_default' => 0))
                    ->execute();
            }
            return array('id' => $info->id);
        } else {
            throw new Exception('保存失败');
        }
    }


    public function delete(int $mid, int $id): bool
    {
        $row = DB::delete($this->table_name)
            ->where('memberid', '=', $mid)
            ->where('id', '=', $id)
            ->execute();
        return (bool)$row;
    }
}
